<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../css/mui.css" rel="stylesheet"/>
    <link href="../css/header.css" rel="stylesheet"/>
    <style type="text/css">
        #scan {
            width: 100%;
            top: 44px;
            bottom: 0px;
            position: absolute;
            text-align: center;
            background: #000000;
        }
    </style>
</head>

<body>
<!--头部标题栏 -->
<header class="mui-bar mui-bar-nav title">
    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left title-color"></a>
    <h1 class="mui-title title-color"><b>搜一扫</b></h1>
</header>
<div id="scan"></div>
<script src="../js/mui.js"></script>
<script src="../js/app.js"></script>
<script type="text/javascript">
    mui.init();
    mui.plusReady(function () {
        setTimeout("startScan()", 500);
    });

    var scan;

    function startScan() {
        //定义扫码框及扫码条的颜色样式
        var styles = {
            background: "",
            frameColor: "#ffffff",
            scanbarColor: "#ffffff"
        };
        scan = new plus.barcode.Barcode('scan', null, styles);
        scan.onmarked = onmarked;
    }

    function onmarked(type, result) {
        if (type == 0) {
            //对扫描结果进行字符串拆分
            var res = result.split("bird_qrcode:");
            if (res.length != 2) {
                alert(result);
            } else {
                var user = app.getUserGlobalInfo();
                var friendName = res[1];
                plus.nativeUI.showWaiting("请稍后");
                // 与后端联调
                mui.ajax(app.serverUrl + "/user/searchFriend?myUserId=" + user.id + "&friendUserName=" + friendName, {
                    type: 'post',//HTTP请求类型
                    timeout: 10000,//超时时间设置为10秒；
                    headers: {'Content-Type': 'application/json'},
                    success: function (data) {
                        // 关闭等待框
                        plus.nativeUI.closeWaiting();
                        if (data.status == 200) {
                            console.log(JSON.stringify(data.data));
                            mui.openWindow({
                                url: "searchFriendsResult.html",
                                id: "searchFriendsResult.html",
                                extras: {
                                    friend: data.data
                                }
                            });
                        } else {
                            app.showToast(data.msg, "error");
                        }
                    }
                });
            }
        }
        scan.start();
    }
</script>
</body>

</html>
